package ph.com.gs3.formalistics.presenter;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.global.constants.NotificationType;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.communicators.HttpCommunicator;
import ph.com.gs3.formalistics.model.communicators.HttpCommunicatorModule.CommunicatorException;
import ph.com.gs3.formalistics.model.communicators.NotificationCommunicatorModule;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse.APIInvalidResponseException;
import ph.com.gs3.formalistics.model.valueobjects.application.CommentNotification;
import ph.com.gs3.formalistics.model.valueobjects.application.Notification;
import ph.com.gs3.formalistics.model.valueobjects.application.RequestNotification;
import ph.com.gs3.formalistics.model.valueobjects.business.User;
import ph.com.gs3.formalistics.presenter.fragments.views.NotificationViewFragment;
import ph.com.gs3.formalistics.presenter.fragments.views.NotificationViewFragment.NotificationViewActionListener;
import ph.com.gs3.formalistics.service.managers.SessionManager;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

public class NotificationActivity extends Activity implements NotificationViewActionListener {

	public static final String TAG = NotificationActivity.class.getSimpleName();

	public static final String EXTRA_CURRENT_USER = "current_user";

	private NotificationViewFragment viewFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notification);

		if (savedInstanceState == null) {
			viewFragment = new NotificationViewFragment();
			getFragmentManager().beginTransaction().add(R.id.container, viewFragment).commit();
		}

		testNotifications();

	}

	/**
	 * For testing only
	 */
	public void testNotifications() {

		AsyncTask<Void, Void, Void> testTask = new AsyncTask<Void, Void, Void>() {

			List<Notification> notifications = new ArrayList<>();

			@Override
			protected Void doInBackground(Void... params) {

				User activeUser = SessionManager.getApplicationInstance()
				        .getLastActiveUserFromDatabase();

				String testServer = activeUser.getAccountServer();
				HttpCommunicator communicator = new HttpCommunicator();

				NotificationCommunicatorModule communicatorModule = new NotificationCommunicatorModule(
				        communicator, testServer);

				APIResponse response = null;

				try {
					response = communicatorModule.getNotifications(0, 20);
					FLLogger.d(TAG, response.getResults());
				} catch (CommunicatorException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (APIInvalidResponseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (response.isOperationSuccessful()) {

					try {
						JSONArray responseJSONArray = new JSONArray(response.getResults());

						for (int i = 0; i < responseJSONArray.length(); i++) {
							JSONObject notificationJSON = responseJSONArray.getJSONObject(i);

							String rawType = notificationJSON.getString("notification_type");
							NotificationType type = Notification
							        .getNotificationTypeFromString(rawType);

							Notification notification = null;

							if (type == null) {
								FLLogger.e(TAG, "Error - null type: " + rawType);
								continue;
							}

							switch (type) {
								case NEW_REQUEST_COMMENT:
									notification = new CommentNotification();
									break;
								case NEW_REQUEST_FOR_APPROVAL:
									notification = new RequestNotification();
									break;
								default:
									continue;
							}

							notification.initializeFromJSON(notificationJSON);
							notifications.add(notification);
						}

					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				super.onPostExecute(result);
				viewFragment.setNotifications(notifications);
			}

		};

		testTask.execute();

	}
}
